using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using System.IO;

namespace December
{
    public class Game2 : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;

        IntPtr DRAW_SURFACE;
        CharEditor PARENT_FORM;
        customPictureBox SURFACE_PICTURE_BOX;
        System.Windows.Forms.Control GAME_FORM;

        public Texture2D ICON_LINES;

        public XNACharEditorHelper XNA_CHAR_EDITOR_HELPER;

        public Game2(IntPtr drawSurface, CharEditor parentForm, customPictureBox surfaceBox)
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";

            this.IsMouseVisible = false;
            this.DRAW_SURFACE = drawSurface;
            this.PARENT_FORM = parentForm;
            this.SURFACE_PICTURE_BOX = surfaceBox;
            
            graphics.PreparingDeviceSettings += new EventHandler<PreparingDeviceSettingsEventArgs>(graphics_PreparingDeviceSettings);
            Mouse.WindowHandle = drawSurface;

            GAME_FORM = System.Windows.Forms.Control.FromHandle(this.Window.Handle);
            GAME_FORM.VisibleChanged += new EventHandler(gameForm_VisibleChanged);
            GAME_FORM.SizeChanged += new EventHandler(gameForm_SizeChanged);
            SURFACE_PICTURE_BOX.SizeChanged += new EventHandler(gameForm_SizeChanged);
            SURFACE_PICTURE_BOX.MouseWheel += new System.Windows.Forms.MouseEventHandler(surfacePictureBox_MouseWheel);
            SURFACE_PICTURE_BOX.MouseMove += new System.Windows.Forms.MouseEventHandler(surfacePictureBox_MouseMove);
        }

        void surfacePictureBox_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
        {
            if (XNA_CHAR_EDITOR_HELPER.DRAW_LINE_DRAG_CURSOR)
            {
                SURFACE_PICTURE_BOX.Cursor = System.Windows.Forms.Cursors.Hand;
            }
            else
            {
                SURFACE_PICTURE_BOX.Cursor = System.Windows.Forms.Cursors.Default;
            }
        }

        #region GDI+ event listeners
        int mousedelta = 0;
        void surfacePictureBox_MouseWheel(object sender, System.Windows.Forms.MouseEventArgs e)
        {
            if (e.Delta > 0)
            {
                mousedelta = 1;
            }
            if (e.Delta < 0)
            {
                mousedelta = -1;
            }
        }

        void gameForm_SizeChanged(object sender, EventArgs e)
        {
            if (PARENT_FORM.WindowState != System.Windows.Forms.FormWindowState.Minimized)
            {
                graphics.PreferredBackBufferWidth = SURFACE_PICTURE_BOX.Width;
                graphics.PreferredBackBufferHeight = SURFACE_PICTURE_BOX.Height;
                graphics.ApplyChanges();
                if (XNA_CHAR_EDITOR_HELPER != null)
                {
                    XNA_CHAR_EDITOR_HELPER.updateResize(graphics.PreferredBackBufferWidth, graphics.PreferredBackBufferHeight);
                }
            }
        }

        void gameForm_VisibleChanged(object sender, EventArgs e)
        {
            if (GAME_FORM.Visible)
                GAME_FORM.Visible = false;
        }

        void graphics_PreparingDeviceSettings(object sender, PreparingDeviceSettingsEventArgs e)
        {
            e.GraphicsDeviceInformation.PresentationParameters.DeviceWindowHandle = DRAW_SURFACE;
        }
        #endregion

        protected override void Initialize()
        {
            // TODO: Add your initialization logic here
            base.Initialize();
        }

        protected override void LoadContent()
        {
            // Create a new SpriteBatch, which can be used to draw textures.
            spriteBatch = new SpriteBatch(GraphicsDevice);

            XNA_CHAR_EDITOR_HELPER = new XNACharEditorHelper(PARENT_FORM.FONT,PARENT_FORM.CHARACTER, graphics.PreferredBackBufferWidth, graphics.PreferredBackBufferHeight, GraphicsDevice);
            XNA_CHAR_EDITOR_HELPER.LoadContent(Content);
            // TODO: use this.Content to load your game content here
            ICON_LINES = Content.Load<Texture2D>(@"Buttons\linesIcon");
            PARENT_FORM.initButtonImages();
        }

        protected override void UnloadContent()
        {
            // TODO: Unload any non ContentManager content here
        }

        protected override void Update(GameTime gameTime)
        {
            // Allows the game to exit
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                this.Exit();

            XNA_CHAR_EDITOR_HELPER.Update(gameTime, Keyboard.GetState(), Mouse.GetState(), mousedelta);
            mousedelta = 0;
            // TODO: Add your update logic here
            base.Update(gameTime);
        }

        protected override void Draw(GameTime gameTime)
        {
            int nA = PARENT_FORM.FONT.Background.A;
            int nR = PARENT_FORM.FONT.Background.R;
            int nG = PARENT_FORM.FONT.Background.G;
            int nB = PARENT_FORM.FONT.Background.B;

            GraphicsDevice.Clear(new Color(nR,nG,nB, nA));
            // TODO: Add your drawing code here
            XNA_CHAR_EDITOR_HELPER.Draw(spriteBatch);

            base.Draw(gameTime);
        }
    }
}
